package com.example.lhsama.config;

import com.example.lhsama.security.MyAccessDecisionManager;
import com.example.lhsama.security.MyInvocationSecurityMetadataSourceService;
import com.example.lhsama.security.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)  //  启用方法级别的权限认证
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private MyUserDetailsService MyUserDetailsService;
    @Autowired
    private MyInvocationSecurityMetadataSourceService myInvocationSecurityMetadataSourceService;
    @Autowired
    private MyAccessDecisionManager myAccessDecisionManager;


    //AuthenticatManager的配置
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(MyUserDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .formLogin()
                .loginPage("/login.html")    //登录页面设置
                .loginProcessingUrl("/login")   //登录访问的路径
                .defaultSuccessUrl("/main.html",true)     //登录成功后,跳转路径
                .permitAll()
                .and()
            .authorizeRequests()      //开启登录选择认证
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O o) {
                        o.setSecurityMetadataSource(myInvocationSecurityMetadataSourceService);
                        o.setAccessDecisionManager(myAccessDecisionManager);
                        return o;
                    }
                })
                .antMatchers("/css/**","/fonts/**","/images/**","/js/**","/templates/error/**").permitAll()  //设置哪些路径不需要认证,这里也能放行静态资源
                .anyRequest().authenticated()         //表示剩余其他接口,登录之后就能访问
                .and()
            .csrf()
                .disable()//关闭csrf防护

            .logout()
                .logoutSuccessUrl("/login");




    }


}
